﻿using System.IO;
using System.Text.RegularExpressions;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using UnityEditor;
using UnityEngine;

namespace EGConfig
{
    public class shiExcelEditor : EditorWindow
    {
        [MenuItem("Assets/添加到音效表")]
        public static void CreateCSharpConfigData()
        {
            var path = Application.dataPath + "/ExcelConfig/sound.xlsx";
            var workbook = new XSSFWorkbook(File.OpenRead(path));
            var sheet = workbook.GetSheetAt(0);
            for (var i = 0; i < Selection.objects.Length; i++)
            {
                var obj = Selection.objects[i];
                AudioClip audioClip = obj as AudioClip;
                var row = sheet.CreateRow(sheet.LastRowNum + 1);

                row.CreateCell(0).SetCellValue(audioClip.name);
                row.CreateCell(1).SetCellValue(audioClip.length.ToString());
            }


            FileStream fs = new FileStream(path, FileMode.Create,
                FileAccess.ReadWrite);
            workbook.Write(fs);
            fs.Close();

            AssetDatabase.Refresh();
            Debug.Log("添加完成！");
        }

        private static string GetObjectPath(Object obj)
        {
            string path = AssetDatabase.GetAssetPath(obj);
            path = $"{Application.dataPath}/../{path}";
            return path;
        }
    }
}